package org.dydl.service.system.impl;

import org.apache.commons.lang.StringUtils;
import org.dydl.common.dto.ResponseData;
import org.dydl.common.dto.ResponseMessage;
import org.dydl.dao.system.repository.IMaterialRepertoryRepository;
import org.dydl.model.system.Goods;
import org.dydl.service.system.IMaterialRepertoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;

@Service("materialRepertoryService")
public class MaterialRepertoryServiceImpl implements IMaterialRepertoryService {

	@Autowired
	private IMaterialRepertoryRepository materialRepertoryRepository;

	@Override
	public ResponseMessage save(Goods goods) {
		Goods save = materialRepertoryRepository.save(goods);
		ResponseMessage res = new ResponseMessage(true, save);
		return res;
	}

	@Override
	public ResponseMessage delete(int id) {
		materialRepertoryRepository.delete(id);
		ResponseMessage res = new ResponseMessage(true);
		return res;
	}

	@Override
	public ResponseMessage getMaterialRepertoryList(int page, int rows, String name ) {
		Pageable pageable = new PageRequest(page - 1, rows);
		Page<Goods> pageList = null;

		if (StringUtils.isNotBlank(name)) {
			pageList = materialRepertoryRepository
					.getMaterialRepertoryListLike(String.format("%s%s%s", "%", StringUtils.trim(name), "%"), pageable);
		} else {
			pageList = materialRepertoryRepository.getMaterialRepertoryList(  pageable);
		}

		ResponseMessage responseMessage = new ResponseMessage(true, new ResponseData<Goods>(page,
				pageList.getTotalPages(), ((int) pageList.getTotalElements()), pageList.getContent()));

		return responseMessage;
	}


}
